<?php

namespace app\api\controller\system;
use app\api\service\access\RoleMenuService;
use app\BaseController;
use app\api\service\access\MenuService;
use app\util\CommonUtil;
use app\api\service\develop\DatagridColumnService;
use app\api\service\log\AdminOperateLogService;
use app\util\AdminUserUtil;
use app\util\ResponseUtil;

class Menu extends BaseController
{
    public function getMenuInfo()
    {
        $id = $this->requestData['id'];
        $service = new MenuService();
        $finalData = $service->getMenuInfoById($id);
        return ResponseUtil::success($finalData);
    }

    public function getMenuList()
    {
        $pageIndex = $this->requestData['page'];
        $pageSize = $this->requestData['rows'];
        $service = new MenuService();
        $list = $service->getListWithPage($this->requestData, $pageIndex, $pageSize);
        $total = $service->getTotalNum($this->requestData);
        $finalData = CommonUtil::getPaginationData($list, $total);
        return ResponseUtil::success($finalData);
    }

    public function getMenuTreeList()
    {
        $roleId = $this->requestData['role_id'] ?? 0;
        $service = new MenuService();
        $list = $service->getTreeList($roleId);
        return ResponseUtil::success($list);
    }

    public function getMenuOption()
    {
        $service = new MenuService();
        $list = $service->getListNoPage($this->requestData);
        return ResponseUtil::success($list);
    }

    public function addMenu()
    {
        try{
            if(empty($this->requestData['page_code'])){
                return ResponseUtil::failure("页面标识码不可为空");
            }
            $pageCode = $this->requestData['page_code'];

            $serviceDatagridColumn = new DatagridColumnService();
            $columnList = $serviceDatagridColumn->getDatagridColumnOption(['page_code' => $pageCode, 'is_in_add'=>1, 'is_require' => 1]);
            foreach($columnList as $columnItem){
                if(empty($this->requestData[$columnItem['column_name']])){
                    return ResponseUtil::failure($columnItem['column_title']."不可为空");
                }
            }

            $arrFieldTitle = array();
            $service = new MenuService();
            $res = $service->createRow($this->requestData);
            if($res){
                $addInfo = CommonUtil::getDataInsertMessage($res, $arrFieldTitle);
                $message = '新增菜单,'.$addInfo;
                $logData = [
                    'subject_id' => $res['new_id'],
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('Menu', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("新增菜单失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("新增菜单失败");
        }
    }

    public function editMenu()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的菜单");
            }

            $id = $this->requestData['id'];
            $service = new MenuService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }

            $arrFieldTitle = ['status'=>'状态'];
            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $arrFieldTitle);
                $message = '编辑菜单,'.$changeInfo['update_info'];
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('Menu', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑菜单失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑菜单失败");
        }
    }

    public function deleteMenu()
    {
        try{
            $id = $this->requestData['id'];
            $service = new MenuService();
            $info = $service->findRowById($id);
            if(!$info){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->removeRowById($id);
            if($res){
                $message = '删除菜单：';
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('Menu', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除菜单失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除菜单失败");
        }
    }

    public function deleteMultiMenu(){
        try{
            $id = $this->requestData['id'];
            if(!is_array($id)){
                return ResponseUtil::failure("参数格式有误");
            }
            $service = new MenuService();
            $deleteData = [];
            foreach($id as $value){
                $info = $service->findRowById($value);
                if(!$info){
                    return ResponseUtil::failure("数据不存在");
                }
                $deleteData[] = $info;
            }
            $res = $service->removeListByManyId($id);
            if($res){
                $message = '删除菜单';
                $logData = [
                    'subject_id' => json_encode($id),
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('Menu', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除菜单失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除菜单失败");
        }
    }

    /**
     * Desc: 获取后台面板菜单数据
     * Datetime: 2023/5/8 14:24 by zdb
     */
    public function getShowList()
    {
        try{
            $adminRoleId = AdminUserUtil::getAdminUserLoginInfo()['role_id'];
            $serviceRoleMenu = new RoleMenuService();
            $listRoleMenu = $serviceRoleMenu->getListNoPage(['role_id' => $adminRoleId]);
            $listUserMenu = array_column($listRoleMenu, 'menu_id');
            $serviceMenu = new MenuService();
            $data = $serviceMenu->getPanelMenuList($listUserMenu);
            return ResponseUtil::success($data);
        }catch(\Exception $e){
            $errorMessage = $e->getMessage()."(Line:".$e->getLine()." in ".$e->getFile().")";
            CommonUtil::addErrorLog('Menu', 'getShowList', $errorMessage);
            return ResponseUtil::failure("获取菜单列表失败");
        }
    }

}



